package com.kds.changzhou.handler;

import cn.hutool.core.date.DateUtil;
import com.kds.changzhou.data.Gateway;
import com.kds.changzhou.message.inbound.TimeMessage;
import com.kds.changzhou.netty.TcpClient;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.Attribute;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 孔德嵩
 * 创建日期 2023/12/11
 */
@Slf4j
public class TimeHandler extends SimpleChannelInboundHandler<TimeMessage> {


    private HandlerConfig handlerConfig;

    public TimeHandler(HandlerConfig handlerConfig) {
        this.handlerConfig = handlerConfig;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TimeMessage msg) throws Exception {


        Attribute GATE_WAY = ctx.channel().attr(TcpClient.GATE_WAY_KEY);

        Gateway gateway = (Gateway) GATE_WAY.get();

        log.info("服务器返回对采集器[{}_{}]时钟同步设置,当前系统时间:{}", gateway.getBuildingCode(), gateway.getGatewayId(), DateUtil.toLocalDateTime(msg.getTime()));
    }
}
